<template>
  <div class="content">
    <div
      class="title font-16 font-bold"
      v-text="$t('label433')"
    ></div>
    <div class="timebuy_box">
      <div>
        <div class="top_btnbox">
          <el-row class="padb15">
            <el-button type="primary" icon="el-icon-tickets"  @click="navigateTo('/my/arpointBuyList')">点卡购买记录</el-button>
            <el-button type="warning" class="m15" icon="el-icon-document"  @click="navigateTo('/my/arPointuseList')">点卡支付明细记录</el-button>
          </el-row>
        </div>
        <el-row :gutter="20">
          <el-col :span="5" v-for="(item,index) in listData" :key="index" class="t_bord list_info" v-bind:class='{active:index==isactive}'>
            <div class="grid-content bg-purple"  @click="addActive(item,index)" >
            <div>
              <div class="ti_top back_hui" data-id="item.autoid">
                <h2>{{Number(item.point)}} 点套餐</h2>
              </div>
              <div>
                <h3 class="font-24 padd20">{{Number(item.point)}} 点</h3>
                <el-button type="danger">限送 {{Number(item.coinnumber)}} {{coinName}} </el-button>
                <p class="padd20">可抵扣等值手续费 {{Number(item.point)}} USDT</p>
              </div>
              <div class="ti_foot back_hui">
                <h3 class="t_red"><span class="m-right-10">售价  </span> <span class="font-24 font-bold ">{{Number(item.usdt)}}  </span> <span class="m-right-10">USDT</span></h3>
              </div>
            </div>
          </div>
          </el-col>
        </el-row>
      </div>
      <div class="buy_form">
        <el-form ref="form" :model="sizeForm" label-width="80px" size="medium" :label-position="labelPosition">
          <el-form-item label="份数">
            <el-input v-model="sizeForm.number" @input="descInput"></el-input>
          </el-form-item>
          <el-form-item label="支付币种">
            <ul class="coin_list">
              <li v-for="(item,index) in listPayData" :key="index"  class="coinname" @click="clickRadio(item,index)"  v-bind:class='{active:index==isactive2}'>
                <p>{{item.coin}}</p>
              </li>
            </ul>
          </el-form-item>
          <el-form-item label=''>
            <p>应支付金额 (以系统收到下单时的汇率为准)</p>
            <h3 class="font-24"> ≈{{Number(canpayMoney)||'0'}} {{usableCoinName}}</h3>
            <p> 可用 {{Number(usable)}} {{usableCoinName}}</p>
          </el-form-item>
          <el-form-item size="large">
            <el-button type="danger" @click="BycointimBtn">立即购买</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import VueQr from "vue-qr";
import html2canvas from 'html2canvas';
export default {
  components: { VueQr },
  data() {
    return {
      userData: "",
      showLoading: true,
      labelPosition:'top',
      coinName:'AR',
      listData:[],
      listPayData:[],
      isactive:0,
      isactive2:'0',
      canpayMoney:'0',
      ItemUsdt:'',
      sizeForm: {
          pointid: '',
          coinid: '',
          number: '1',
      },
      usableCoinName:'',
    };
  },
  mounted() {
    this.showLoading = true;
    this.getpointlist();
    this.getpointcoinlist();
  },
  methods: {
    test(dataUrl,id){
            console.log(url, id)
        },
    handleSelect(key) {
      this.navigateTo(key);
    },
    onSuccess() {
      this.successMsg("复制成功");
    },
    // 查看点卡购买方案
    getpointlist(){
      this.request(this.api.getpointlist).then(res => {
        try {
          this.listData = res.data.result;
          this.sellPrice=res.data.result[0].usdt;
          this.sizeForm.pointid=res.data.result[0].autoid;
        } catch (err) {}
      })
    },
    // 支持币种及单价
    getpointcoinlist(){
      this.request(this.api.getpointcoinlist).then(res => {
        try {
          this.listPayData = res.data.result;
          this.usable=this.listPayData[0].usable;
          this.usableCoinName=this.listPayData[0].coin;
          this.ItemUsdt=this.listPayData[0].usdt;
          const payUsdt=Number(this.sellPrice)/Number(this.ItemUsdt)*Number(this.sizeForm.number);
          this.canpayMoney=payUsdt.toFixed(8);  
        } catch (err) {}
      })
    },
    addActive:function(item,index){
				//点击切换 变量的值 赋值为 index
				console.log(index);
        this.isactive=index;
        this.sizeForm.pointid=item.autoid;
        this.sellPrice=item.usdt;
        const payUsdt=Number(this.sellPrice)/Number(this.ItemUsdt)*Number(this.sizeForm.number.trim());
        this.canpayMoney=payUsdt.toFixed(8);
    },
    clickRadio:function(item,index){
      console.log(item);
      console.log(index);
      this.isactive2=index;
      var i=index;
      this.usable=item.usable;
      this.usableCoinName=item.coin;
      this.ItemUsdt=item.usdt;
      console.log(this.sizeForm.number+'this.sellPrice'+this.sellPrice+'item.usdt'+item.usdt);
      console.log(Number(this.sizeForm.number)+'sellPrice'+Number(this.sellPrice)+'usdt'+Number(item.usdt)+'usdt'+Number(item.usdt)+'A'+Number(this.sizeForm.number))
      // this.canpayMoney=Number(this.sizeForm.number)*Number(this.sellPrice)*Number(item.usdt)/Number(item.usdt)*Number(this.sizeForm.number)
      const payUsdt=Number(this.sellPrice)/Number(this.ItemUsdt)*Number(this.sizeForm.number.trim());
      this.canpayMoney=payUsdt.toFixed(8);
    },
    descInput() {
      console.log(123);
      const payUsdt=Number(this.sellPrice)/Number(this.ItemUsdt)*Number(this.sizeForm.number.trim());
      this.canpayMoney=payUsdt.toFixed(8);
    },
    BycointimBtn(){
      if(this.sizeForm.number<=0){
        this.$message.error('输入的数量错误');
        this.sizeForm.number=0;
        this.canpayMoney=0;
        return
      }
      const buyData={
        number:this.sizeForm.number.trim(),
        pointid:this.sizeForm.pointid,
        coinid:this.usableCoinName,
      }
      // 弹窗二次确认
      this.$confirm('确定要购买点卡吗？ 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.request(this.api.buypoint,buyData).then(res => {
            try {
              if(res.code)
              this.$message({
                message: res.msg,
                type: 'success'
              });
              this.sizeForm.number=0;
              this.getpointcoinlist();
            } catch (err) {
              this.errMsg(res.msg || "操作失败");
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
      
    },
  }
};
</script>
<style lang="scss" scoped>
.table-wrap {
  border: $default-border;
  border-bottom: none;
}
.title {
  margin-bottom: 25px;
}
.bot_top{
    border-top: 1px solid #c1d0cd;
}
.padd20{padding: 15px 0;}
.text_cen{text-align: center;}
.m20{margin-top: 20px;}
.t_bord{border: 1px solid #f3f3f3;}
.back_hui{background: #eeeeee;height: 50px;line-height: 50px;}
.list_info{text-align: center;padding: 0!important;margin-right: 30px;}
.buy_form{width: 600px;margin: 60px auto 0 auto;}
.t_red{color: $color-danger}
.active{border: 2px solid $main-bg-color;}
.coin_list{
  display: flex;text-align: center;flex-wrap: wrap ;
  li{width: 80px;margin:0 15px 15px 0;}
  p{border: 1px solid #eeeeee;}
}
// topbtn
.top_btnbox{
  margin: 20px 0;border-bottom:1px solid #eeeeee;
  .m15{margin-left: 15px;}
  .padb15{padding-bottom: 15px;}
  }
.el-row{margin-left: 0!important}
</style>
